package cn.wolfcode.wolf2w.job;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.util.Rediskeys;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RedisDataPersistenceJob {
    @Autowired
    private IStrategyService strategyDetailService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisRedisService;
    //秒 分 小时 月份中的日期 月份 星期中的日期 年份
    //0  0   0       1/1     *     ?
//    @Scheduled(cron = "* * * 1/1 * ?")
    public void redisDataPersistence() {
        System.out.println("15s");
        String keyPattern = Rediskeys.STRATEGY_STATIS_VO.getPrefix() + ":*";
        List<StrategyStatisVO> list = strategyStatisRedisService.getStatisVOs(keyPattern);
        for (StrategyStatisVO vo : list) {
            Strategy strategy = strategyDetailService.get(vo.getStrategyId());
            BeanUtils.copyProperties(vo, strategy);
            strategyDetailService.update(strategy);
        }
    }
}